public class Leetcode392 {
    public boolean isSubsequence(String s, String t) {
        char[] chars = s.toCharArray();

        int index = -1;
        for (char c : chars) {
            index = t.indexOf(c, index + 1);
            if (index == -1) {
                return false;
            }
        }

        return true;
    }

    /**
     * 贪心
     *
     * @param s
     * @param t
     * @return
     */

    public static boolean isSubsequence1(String s, String t) {
        char[] sChars = s.toCharArray();
        char[] tChars = t.toCharArray();
        int sIndex = 0;
        int tIndex = 0;
        while (sIndex < sChars.length && tIndex < tChars.length) {
            if (sChars[sIndex] == tChars[tIndex]) {
                sIndex++;
            }

            tIndex++;
        }

        return sIndex == sChars.length;
    }

    public static void main(String[] args) {
        System.out.println(isSubsequence1("abc",
                "ahbgdc"));
    }
}
